package com.fyzk.activity.lottery.basketball;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.pop.ViewHolder;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;

public class JCLQFilterGameQueuePop extends PopupWindow implements
		OnClickListener {

	private Button cancel, ok;

	private GridView jc_filter_games;

	private ArrayList<String> teams = new ArrayList<String>();
	private ArrayList<String> teams_sel = new ArrayList<String>();

	private Context context;

	private LinearLayout filter_choose_help;
	
	private Button filter_select_all,filter_unselect_all;

	public JCLQFilterGameQueuePop(Context context, int width, int height) {
		super(width, height);
		this.context = context;
		View view = View.inflate(context, R.layout.layout_filtergamequeue_jclq,
				null);
		this.setContentView(view);
		this.setOutsideTouchable(true);
		// 设置此参数获得焦点，否则无法点击
		this.setBackgroundDrawable(context.getResources().getDrawable(
				R.color.transparent));
		this.setFocusable(true);
		cancel = (Button) view.findViewById(R.id.filter_cancel);
		ok = (Button) view.findViewById(R.id.filter_ok);
		cancel.setOnClickListener(this);
		ok.setOnClickListener(this);
		filter_choose_help = (LinearLayout) view
				.findViewById(R.id.filter_choose_help);
		filter_select_all = (Button)view.findViewById(R.id.filter_select_all);
		filter_unselect_all = (Button)view.findViewById(R.id.filter_unselect_all);
		filter_unselect_all.setOnClickListener(checkedChangeListener);
		filter_select_all.setOnClickListener(checkedChangeListener);
//		filter_choose_help.setOnCheckedChangeListener(checkedChangeListener);
		jc_filter_games = (GridView) view.findViewById(R.id.jc_filter_games);
	}

	private OnClickListener checkedChangeListener = new OnClickListener() {


		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.filter_select_all:
				for (FilterItem item : items) {
					item.isSel = true;
				}
				adapter.notifyDataSetChanged();
				break;

			case R.id.filter_unselect_all:
				for (FilterItem item : items) {
					item.isSel = !item.isSel;
				}
				adapter.notifyDataSetChanged();
				break;
			}
		}
	};

	FilterAdapter adapter;

	ArrayList<FilterItem> items;

	public void setGames(ArrayList<String> teams, ArrayList<String> teams_sel) {
		this.teams = teams;
		this.teams_sel = teams_sel;
		if (this.teams_sel.size() == 0) {
			this.teams_sel.addAll(teams);
		}
		items = new ArrayList<JCLQFilterGameQueuePop.FilterItem>();
		for (String team : teams) {
			FilterItem item = new FilterItem();
			item.team = team;
			item.isSel = teams_sel.contains(team);
			items.add(item);
		}
		adapter = new FilterAdapter(items);
		jc_filter_games.setAdapter(adapter);
	}

	public class FilterItem {

		public String team;
		public boolean isSel;

		@Override
		public boolean equals(Object o) {
			if(o == null){
				return false;
			}
			FilterItem item = (FilterItem) o;
			return item.team.equals(this.team);
		}

	}

	class FilterAdapter extends BaseAdapter implements
			CompoundButton.OnCheckedChangeListener {

		public List<FilterItem> list = new ArrayList<FilterItem>();
		private int count = 0;

		public FilterAdapter(List<FilterItem> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View view, ViewGroup parent) {
			ViewHolder holder = null;
			if (view == null) {
				holder = new ViewHolder();
				view =  View.inflate(context,
						R.layout.layout_checkbox_filtergamequeue, null);
				holder.checkBox = (CheckBox) view.findViewById(R.id.checkbox);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) (view.getTag());
			}

			FilterItem item = list.get(position);
			holder.checkBox.setOnCheckedChangeListener(null);
			holder.checkBox.setChecked(item.isSel);
			holder.checkBox.setOnCheckedChangeListener(this);
			holder.checkBox.setText(item.team);
			return view;
		}

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			String team = buttonView.getText().toString();
			FilterItem item = new FilterItem();
			item.isSel = false;
			item.team = team;
			int position = list.indexOf(item);
			list.get(position).isSel = isChecked;
		}

	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAsDropDown(parent);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.filter_cancel:
			this.dismiss();
			break;
		case R.id.filter_ok:
			teams_sel.clear();
			for (FilterItem item : items) {
				if (item.isSel) {
					teams_sel.add(item.team);
				}
			}
			if (teams_sel.size() == 0) {
				AppToast.getToast().show("至少选择一个赛事！");
				return;
			}
			if (this.listener != null) {
				listener.onFilter(teams_sel);
			}
			this.dismiss();
			break;

		default:
			break;
		}
	}

	private OnFilterListener listener;

	public void setOnFilterListener(OnFilterListener listener) {
		this.listener = listener;
	}

	public interface OnFilterListener {
		void onFilter(ArrayList<String> teams_sel);
	}

}
